package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.INewsTypeDao;
import com.cn.wanxi.dao.impl.NewsTypeDaoImpl;
import com.cn.wanxi.model.NewsTypeModel;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.service.INewsTypeService;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewsTypeServiceImpl implements INewsTypeService {

    INewsTypeDao newsTypeDao = new NewsTypeDaoImpl();

    @Override
    public NewsTypeModel getNewsType(int id) {
        return newsTypeDao.getNewsType(id);
    }

    @Override
    public int addNewsType(NewsTypeModel newsType) {
        return newsTypeDao.addNewsType(newsType);
    }

    @Override
    public int editNewsType(NewsTypeModel newsType) {
        return newsTypeDao.editNewsType(newsType);
    }

    @Override
    public int deleteNewsType(int id) {
        return newsTypeDao.deleteNewsType(id);
    }

    @Override
    public List<NewsTypeModel> getNewsTypeList() {
        Jedis jedis = new Jedis();
        List<NewsTypeModel> list = null;
        try {
            if (jedis.exists("news_type_id")) {
                list = newsTypeDao.getNewsTypeListByRedis();
            } else {
                list = newsTypeDao.getNewsTypeList();
                newsTypeDao.setNewsTypeListInRedis(list);
            }
        } catch (Exception e) {
            list = newsTypeDao.getNewsTypeList();
        }
        return list;
    }

    @Override
    public PageModel<NewsTypeModel> getNewsTypes(Map<String,Object> map) {
        PageModel pager = new PageModel();
        pager.setList(newsTypeDao.getNewsTypes(map));
        pager.setTotal(newsTypeDao.getNewsTypeCount(map));
        return pager;
    }

    @Override
    public int updateShow(int id, int show) {
        NewsTypeModel newsTypeModel = new NewsTypeModel();
        newsTypeModel.setId(id);
        newsTypeModel.setIsShow(show);
        return newsTypeDao.updateShow(newsTypeModel);
    }
}
